﻿using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using S3.DataModel;
using S3.Biz;
using S3.Common;

namespace S3
{
    public partial class MainForm : S3FormBase
    {       
        public MainForm()
        {            
            InitializeComponent();
        }

        public LoginForm LoginFrm { get; set; }

        //Callback by Login form
        public void ConfigSecurity()
        {
            using (var manager = new UserManager())
            {
                User user = manager.GetOne(Constants.StaffName);
                if (user != null)
                {
                    //Cau hinh cho Admin
                    if (user.Role.ToLower() == UserRoleValue.Text[UserRole.Admin].ToLower())
                    {
                        configMenu.Enabled = true;
                        hrMenu.Enabled = true;
                        allStatsMenu.Enabled = true;
                        gameStatsMenu.Enabled = true;
                        revenuMenu.Enabled = true;
                        historyGameMenu.Enabled = true;
                    }

                    //Cai hinh cho Nhan vien
                    if (user.Role.ToLower() == UserRoleValue.Text[UserRole.Staff].ToLower())
                    {
                        configMenu.Enabled = false;
                        hrMenu.Enabled = false;
                        allStatsMenu.Enabled = false;
                        gameStatsMenu.Enabled = false;
                        revenuMenu.Enabled = false;
                        historyGameMenu.Enabled = false;
                    }
                }
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //DuongTV
            //collect garbare collection
            GC.Collect();
            //remove memory
            GC.RemoveMemoryPressure(GC.GetTotalMemory(true));
            //search missing process                        
            //foreach (Process p in Process.GetProcesses())
            //{
            //    if (!string.IsNullOrEmpty(p.ProcessName) && (p.ProcessName.ToLower().Trim() == "s3"))
            //    {
            //        //kill process
            //        p.Kill();
            //    }
            //}
        }
   

        private void MainFormFormClosing(object sender, FormClosingEventArgs e)
        {
            //foreach (Form f in Application.OpenForms)
            //{
            //    if (f.Name != "MainForm")
            //    {
            //        f.Close();
            //    }
            //}
            Application.ExitThread();
            Application.Exit();
        }

      

       
        #region DeskButton_Click

  
        private void historyGameMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TransactionHistoryGame gameHistory = new TransactionHistoryGame();
            gameHistory.Owner = this;
            gameHistory.ShowDialog();
        }

        private void gameTimeMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ManageTimeBlockForm manageTimeBlockForm = new ManageTimeBlockForm();
            manageTimeBlockForm.Owner = this;
            manageTimeBlockForm.ShowDialog();
        }

        private void serviceMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ManageAdditionalItem manageAdditionalItem = new ManageAdditionalItem();
            manageAdditionalItem.Owner = this;
            manageAdditionalItem.ShowDialog();
        }
     
        private void gameMachineMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ManageDesk manageDesk = new ManageDesk();
            manageDesk.Owner = this;
            manageDesk.ShowDialog();
        }

        private void allStatsMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Reports report = new Reports();
            report.ShowDialog();
        }
     
        private void revenuMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //var x = new Report.RevenueReportForm();
            //x.ShowDialog();
        }

        private void gamePlayingMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ReportPlaying playing = new ReportPlaying();
            playing.ShowDialog();
        }
     
        private void swapPeopleMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FinishSession finishSession = new FinishSession();
            finishSession.ShowDialog();
        }

        private void hrMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ManageUsers manageUser = new ManageUsers();
            manageUser.ShowDialog();
        }

        private void passwordMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChangePassword changePass = new ChangePassword();
            changePass.ShowDialog();
        }

        private void registerMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LisenceForm lisence = new LisenceForm();
            lisence.ShowDialog();
        }

        private void aboutMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }

        private void gameStatsMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //var logFrm = new LogForm();
            //logFrm.ShowDialog();        
        }

        private void closeMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Close();
                Application.Exit();
                //Login login = new Login(new MainForm());

                //login.Visible = false;
                //login.ShowDialog();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        #endregion

        private void MainFormShown(object sender, EventArgs e)
        {
            LoadMainPane();
        }     

        private void ShowLoginForm()
        {
            LoginForm form = new LoginForm { Owner = this };
            form.Show();
        }
       
        public void LoadMainPane()
        {
            //Thread.Sleep(10);

            SaleForm saleForm = new SaleForm();

            saleForm.MdiParent = this;

            saleForm.Show();

            MainPaneForm mainPane = new MainPaneForm(this);

            if (mainPane.InvokeRequired)
            {
                mainPane.Invoke((MethodInvoker)delegate { mainPane.MdiParent = this; });
            }
            else
            {
                mainPane.MdiParent = this;
            }
           
            mainPane.Show();

         
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        private void lightStyleMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");
        }

        private void darkStyleMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserLookAndFeel.Default.SetSkinStyle("DevExpress Dark Style");
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserLookAndFeel.Default.SetSkinStyle("Office 2010 Blue");
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserLookAndFeel.Default.SetSkinStyle("Office 2010 Black");
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserLookAndFeel.Default.SetSkinStyle("Office 2010 Silver");
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserLookAndFeel.Default.SetSkinStyle("Caramel");
        }

        private void SaleMenu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new SaleForm().Show();
        }
    }
}